<template>
  <div>
    <!-- 添加固定资产对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="申购单号">
              <el-input class="readonly" :value="detail.odd" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产类别">
              <el-input class="readonly" :value="detail.assetTypeName" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产名称">
              <el-input class="readonly" :value="detail.name" readonly>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="待购数量">
              <el-input class="readonly" :value="detail.number" readonly>
                <template slot="append">{{ detail.unit }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="已购数量">
              <el-input class="readonly" :value="detail.finishNumber" readonly>
                <template slot="append">{{ detail.unit }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购数量" prop="finishNumber">
              <el-input
                type="number"
                v-model="form.finishNumber"
                controls-position="right"
                :min="0"
                :max="detail.number - detail.finishNumber"
                placeholder="请输入采购数量"
                ><template slot="append">{{ detail.unit }}</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="预计价格">
              <el-input class="readonly" :value="detail.price" readonly>
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="8">
            <el-form-item label="采购价格" prop="price">
              <el-input
                type="number"
                v-model="form.price"
                controls-position="right"
                :min="0"
                placeholder="请输入采购价格"
                ><template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="使用年限" prop="useLife">
              <el-input
                type="number"
                v-model="form.useLife"
                controls-position="right"
                :min="0"
                placeholder="请输入使用年限"
                ><template slot="append">年</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="8">
            <el-form-item label="残值率" prop="valueRate">
              <el-input
                type="number"
                v-model="form.valueRate"
                controls-position="right"
                step="0.1"
                precision="0.01"
                :min="0"
                :max="1"
                placeholder="请输入残值率"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="() => (this.open = false)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, add } from "@/api/prod/purchase/process";

export default {
  name: "AddAssetType",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 表单类型
      type: null,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        finishNumber: null,
        id: null,
        price: null,
        useLife: null,
        valueRate: null,
      },
      // 详情
      detail: {},
      // 表单校验
      rules: {},
    };
  },

  computed: {
    // 弹出层标题
    title() {
      if (this.type !== null) return this.type + "-固定资产";
      return "";
    },
  },

  methods: {
    // 表单重置
    reset() {
      this.type = null;
      this.form = {
        finishNumber: null,
        id: null,
        price: null,
        useLife: null,
        valueRate: null,
      };
    },

    /** 弹出框打开 */
    show(type, id) {
      this.reset();
      this.open = true;
      this.type = type;
      this.form.id = id;
      get(id).then((response) => {
        this.detail = response.data;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          add(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.$emit("refresh");
          });
        }
      });
    },
  },
};
</script>

<style>
</style>